/*
 * =====================================================================================
 *
 *       Filename:  net_utils.c
 *
 *    Description:  net utils function
 *
 *        Version:  1.0
 *        Created:  2012年11月29日 18时44分02秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

void hex_echo(uint8_t *data_ptr,uint32_t len)
{
	int j,i=0;
	for(j=len-1;j>=0;j--)
	{
		printf("%02x ",*(data_ptr+j));
		((++i)==8)?(i=0)&&printf("\n"):NULL;
		}
	printf("\n");
}


void binary_echo(uint8_t *data_ptr,uint32_t len)
{
	int i=0,j=0;
	for(j=len-1;j>=0;j--)
	{
		for(i=0;i<8;i++)
		{
			printf("%d",((*(data_ptr+j)<<i)&0x80)?1:0);
		}
		printf(" ");
	}
	printf("\n");
}


void endian_swap(uint8_t **data_ptr,uint32_t len)
{
	uint8_t *data=*data_ptr;

	int i=0;
	uint8_t swap;
	for(i=0;i<len/2;i++)
	{
		swap=*(data+i);
		*(data+i)=*(data+len-i-1);
		*(data+len-i-1)=swap;
	}
}


int main()
{
	uint64_t test=0x1122334455667788;
	hex_echo(&test,8);

	uint8_t *ptr=&test;
	endian_swap(&ptr,8);
	hex_echo(&test,8);

	binary_echo(&test,8);
	return 0;
}
